<template>
    <div class="container">
        <!--查询部分-->
        <!--inline=true水平摆放form表单-->
        <el-form ref="jobQueryForm" :inline="true" :model="jobQueryForm">

            <el-form-item prop="code" size="small" label="岗位编码">
                <el-input clearable v-model="jobQueryForm.code" placeholder="请输入菜单名称"></el-input>
            </el-form-item>

            <el-form-item prop="name" size="small" label="岗位名称">
                <el-input clearable v-model="jobQueryForm.name" placeholder="请输入菜单名称"></el-input>
            </el-form-item>

            <el-form-item prop="status" size="small" label="状态">
                <el-select clearable v-model="jobQueryForm.status" placeholder="岗位状态">
                    <el-option :key="index" v-for="(item,index) in $dictMap('status')" :label="item.desc" :value="item.value"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item size="mini">
                <el-button @click="searchJob" style="width: 80px" type="primary"><i
                        class="iconfont icon-search-line"/><span>查询</span>
                </el-button>
            </el-form-item>

            <el-form-item size="mini">
                <el-button @click="$refs.jobQueryForm.resetFields()" style="width: 80px" type="normal"><i
                        class="iconfont icon-zhongzhi-"/><span>重置</span>
                </el-button>
            </el-form-item>

            <el-form-item size="mini">
                <el-button plain style="width: 80px" type="primary" @click="addJob"><i
                        class="iconfont icon-jia"/><span>新增</span></el-button>
            </el-form-item>
        </el-form>
        <!--查询部分-->


        <!--菜单列表 列表部分-->
        <el-table
            :header-cell-style="{'text-align':'center',background:'#f8f8f9',color:'#606266'}"
            :cell-style="{'text-align':'center'}"
            :data="jobDatas"
            style="width: 100%;margin-bottom: 20px;"
            row-key="id"
            border
            default-expand-all>
            <el-table-column
                prop="name"
                label="角色名称">
            </el-table-column>

            <el-table-column
                prop="code"
                label="权限字符">
            </el-table-column>

            <el-table-column
                prop="sort"
                sortable
                label="排序">
            </el-table-column>

            <el-table-column
                width="200"
                label="状态">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        @change="statusChange(scope.row)"
                        :active-value="'1'"
                        :inactive-value="'0'"
                        active-color="#13ce66"
                        inactive-color="#a9acb8"
                        active-text="开启"
                        inactive-text="禁用">
                    </el-switch>
                </template>
            </el-table-column>

            <el-table-column
                prop="createTime"
                sortable
                label="创建时间">
            </el-table-column>


            <el-table-column
                label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary"
                               @click="editJob(scope.row)" round>修改
                    </el-button>
                    <el-button size="mini" type="danger" @click="delJob(scope.row,scope.$index)" round>删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--菜单列表 列表部分-->


        <!--添加菜单 dialog-->
        <el-dialog @close="jobForm={status:1}" width="600px"  :title="dialogName" :visible.sync="addJobVisible">
            <!--label-width 表单里面label的标签-->
            <el-form ref="jobAddForm" :rules="jobRules" :model="jobForm" label-width="80px">

                <el-form-item prop="name" label="岗位名称">
                    <el-input v-model="jobForm.name"></el-input>
                </el-form-item>

                <el-form-item prop="code" label="权限字符">
                    <el-input v-model="jobForm.code"></el-input>
                </el-form-item>

                <el-form-item prop="sort" label="显示排序">
                    <el-input-number v-model="jobForm.sort" controls-position="right" :min="1"
                                     :max="15"></el-input-number>
                </el-form-item>

                <el-form-item v-if="!jobForm.id" prop="status" label="岗位状态">
                    <el-radio :key="index" v-for="(item,index) in $dictMap('status')" v-model="jobForm.status" :label="item.value">{{item.desc}}</el-radio>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="addJobVisible = false;jobForm={status:1}">取 消</el-button>
                <el-button type="primary" @click="oprJob">确 定</el-button>
            </div>
        </el-dialog>
        <!--添加菜单 dialog-->

        <!-- 分页组件 -->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <!-- 分页组件 -->
    </div>
</template>

<script>

    export default {
        name: "Job",
        inject: ["mainReload"],
        data() {
            return {
                authenticate(name) {
                    return this.$authenticate(this.$route.path,name)
                },

                jobDatas:[],
                jobQueryForm: {status:null,name:'',code:''},
                dialogName: '',
                addJobVisible: false,
                jobForm: {status: 1,code:'',name:'',sort:0},
                jobRules:{
                    name:[
                        { required: true, message: '请输入岗位名称',trigger: 'blur' }
                    ],
                    sort:[
                        { required: true, message: '请选择岗位排序',trigger: 'blur' }
                    ],
                    code:[
                        { required: true, message: '请输入权限编码',trigger: 'blur' }
                    ]
                },

                //分页参数
                total:0,
                pageNum:1,
                pageSize:10
            }
        },
        created() {
            this.$axiosApi.getJobs(this.jobQueryForm,(list,total)=>{
                this.jobDatas = list;
                this.total = total
            })
        },

        //computed:{
        //    ...mapState({
        //        menuTree:"allMenuTree"
        //    }),
        //},

        methods: {
            //分页插件，当前页修改时触发
            handleCurrentChange(pageNum) {
                this.jobQueryForm.pageNum = pageNum
                this.jobQueryForm.pageSize = this.pageSize
                this.$axiosApi.getJobs(this.jobQueryForm, (list) => {
                    this.roleDatas = list
                    this.pageNum = pageNum
                })
            },

            //分页插件，条数修改时触发
            handleSizeChange(pageSize) {
                this.pageNum = 1;
                this.jobQueryForm.pageNum = 1
                this.jobQueryForm.pageSize = pageSize
                this.$axiosApi.getJobs(this.jobQueryForm, (data, total) => {
                    this.jobDatas = data
                    this.total = total
                    this.pageSize = pageSize
                })
            },

            //控制状态的开关按钮触发
            statusChange(job) {
                this.$put("/job/editJob", job).then(() => {
                    if (job.status === '1') {
                        this.$message({
                            message: "已开启",
                            type: 'success'
                        });
                    } else {
                        this.$message("已禁用")
                    }
                })
            },

            //查询模块，搜索按钮
            searchJob() {
                //点击搜索后，要重置pageNum和pageSize，以免被之前的分页参数影响
                this.pageNum = 1
                this.pageSize = 10
                this.jobQueryForm.pageNum = 1;
                this.jobQueryForm.pageSize = 10;
                this.$axiosApi.getJobs(this.jobQueryForm, (list, total) => {
                    this.jobDatas = list
                    this.total = total
                })
            },

            delJob(job,index) {
                this.$confirm(`确定永久删除 '${job.name}' 岗位吗？`, '警告', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    this.$del("/job/delJob", {
                        id: job.id
                    }).then(() => {
                        this.$message("删除成功");
                        this.mainReload();
                        //如果当前页只剩下一条数据的话，就刷新路由，否则删除roleDatas即可
                        //if (this.roleDatas.length <= 1) {
                        //    this.mainReload();
                        //} else {
                        //    this.roleDatas.splice(index,1)
                        //}
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: "已取消删除"
                    });
                });

            },

            addJob() {
                this.addJobVisible=true;
                this.dialogName='添加岗位'
            },

            //点击编辑按钮触发
            editJob(jobForm) {

                this.addJobVisible = true;
                this.jobForm = JSON.parse(JSON.stringify(jobForm));
                this.dialogName = '编辑岗位'

            },

            // 添加/编辑框 点击确认的时候
            oprJob() {
                //form表单规则校验
                this.$refs['jobAddForm'].validate(async (valid) => {
                    if (!valid) {
                        return false
                    }

                    // 该方法用来处理新增还是处理编辑，关键在于查看this.roleForm对象是否具有id，有就是编辑，无这是新增
                    if (!this.jobForm.id) {
                        await this.$post("/job/addJob", this.jobForm)
                    } else {
                        try {
                            await this.$put("/job/editJob", this.jobForm)
                        } catch (e) {
                            this.addRoleVisible = false;
                            return
                        }
                    }

                    // 关闭编辑框
                    this.addRoleVisible = false

                    // 提示框
                    this.$message({
                        message: "操作成功",
                        type: "success",
                    })

                    // 因为编辑的时候可能已经添加了id，记得要清除id
                    this.jobForm.id = null;

                    //重新装载数据 todo key值写成枚举
                    this.mainReload();
                });
            },
        }
    }
</script>

<style scoped>

</style>